﻿Shader "Custom/Unlit/simple_dissipate"
{
	Properties
	{
		_MainTex("Texture", 2D) = "black" {}
		_Mask("Mask",2D) = "white"{}
		_Color("Color",Color) = (1,1,1,1)
		_ShadowStrngth("ShadowStrngth",Range(0,1)) = 1
		_ShakeStrength("ShakeStrength",Range(0,1)) = 0.1
		_ShakeFreq("ShakeFreq",float) = 0.5
	}
		SubShader
		{
			Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
			LOD 100
			Blend SrcAlpha OneMinusSrcAlpha

			CGINCLUDE

			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				float4 vertex : SV_POSITION;
			};

			float2 hash22(float2 p)
			{
				p = float2(dot(p, float2(127.1, 311.7)), dot(p, float2(269.5, 183.3)));
				return -1.0 + 2.0*frac(sin(p)*43758.5453123);
			}

			//perlin
			float perlin_noise(float2 p) {
				float2 pi = floor(p);
				float2 pf = p - pi;
				float2 w = pf * pf*(3.0 - 2.0*pf);
				return lerp(lerp(dot(hash22(pi + float2(0.0, 0.0)), pf - float2(0.0, 0.0)),
					dot(hash22(pi + float2(1.0, 0.0)), pf - float2(1.0, 0.0)), w.x),
					lerp(dot(hash22(pi + float2(0.0, 1.0)), pf - float2(0.0, 1.0)),
						dot(hash22(pi + float2(1.0, 1.0)), pf - float2(1.0, 1.0)), w.x), w.y);
			}

			sampler2D _MainTex;
			sampler2D _Mask;
			float4 _MainTex_ST;
			fixed4 _Color;
			uniform float flipflag;
			uniform float cutoutflag;
			uniform float4 mpos;
			float _ShadowStrngth, _ShakeStrength, _ShakeFreq;

			v2f vertback(appdata v)
			{
				v2f o;

				if (flipflag > 0)
				{
					v.uv.y = 1 - v.uv.y;
				}

				//mpos：鼠标位置相对于图片的位置，以图片中心为零点
				float2 vec = v.vertex.xy - mpos.xy;
				//对显示图片的网格顶点进行缩放
				v.vertex.xy = mpos.xy + vec * 1.05;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
				return o;
			}

			v2f vert(appdata v)
			{
				v2f o;

				if (flipflag > 0)
				{
					v.uv.y = 1 - v.uv.y;
				}

				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
				return o;
			}

			fixed4 fragBack(v2f i) :SV_Target
			{
				// i.uv.x = i.uv.x + sin(_Time.y * 6.28 * _ShakeFreq) * _ShakeStrength * pow(i.uv.y,2) * perlin_noise(float2(_Time.y,i.uv.y));
				fixed4 col = tex2D(_MainTex, i.uv);
				fixed mask = tex2D(_Mask, i.uv).r;
				col.a *= mask;
				//alpha为0的地方全黑
				col.rgb = lerp(fixed3(0,0,0),fixed3(0.05, 0.05, 0.05),col.a);
				col.a *= 0.6;
				return col * _Color * _ShadowStrngth;
			}

			fixed4 frag(v2f i) : SV_Target
			{
				// i.uv.x = i.uv.x + sin(_Time.y * 6.28 * _ShakeFreq) * _ShakeStrength * pow(i.uv.y,2) * perlin_noise(float2(_Time.y,i.uv.y));
				
				float a = sin(i.uv.x * 6.28 * 100);
				float b = sin(i.uv.y * 6.28 * 100);
				clip(a * b);
				
				fixed4 col = tex2D(_MainTex, i.uv);
				fixed mask = tex2D(_Mask, i.uv).r;
				col.a *= mask;
				return col * _Color;
			}
			ENDCG

			Pass
			{
				offset 1,1
				CGPROGRAM
				#pragma vertex vertback
				#pragma fragment fragBack

				ENDCG

			}

			Pass
			{
				CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag

				ENDCG

			}
		}
}
